<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      accept="image/png, image/jpeg"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :before-upload="beforeAvatarUpload"
      :http-request="request"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDFI7f293Yp291f4JXlqgHdqruqcK16xqX',
  SecretKey: 'ZwDE1ZqqeIoBRCn9GBtQkusdCkBZondT'
})
export default {
  name: 'UploadAvator',
  props: {
    staffPhoto: {
      type: Array,
      default: () => []
    }
  },
  components: {},
  data () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      // 预计传送过来的值
      // fileList: [{ name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }, { name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }]
      fileList: []
    }
  },
  computed: {
  },
  // 顺道想到了强制刷新组件的方法
  watch: {
    staffPhoto: {
      deep: true,
      handler (newVal) {
        this.fileList = newVal
      }
    }
  },
  created () {
  },
  mounted () {},
  methods: {
    handleRemove (file, fileList) {
      this.$emit('updata', fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    beforeAvatarUpload (file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    request (file) {
      // 只有当前的对象有内容才可以存在进度条(status,percentage)
      this.fileList.push({ })
      const uploadFile = this.fileList[this.fileList.length - 1]
      uploadFile.status = 'uploading'
      uploadFile.percentage = 0
      cos.uploadFile({
        Bucket: 'myself-1315768506', /* 填写自己的bucket，必须字段 */
        Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
        Key: parseInt(Math.random() * 9999999) + Date.now() + file.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: file.file, // 上传文件对象
        SliceSize: 1024 * 1024 * 5,
        onProgress: function (progressData) {
          uploadFile.status = 'success'
          uploadFile.percentage = progressData.percent * 100
        }
      }).then(data => {
        uploadFile.name = file.file.data
        uploadFile.url = 'http://' + data.Location // 图片不显示的问题
        this.$emit('updata', this.fileList)
        // 数据的处理
      }).catch(err => {
        console.log('上传出错', err)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
